<?php
	/*
	ini_set('display_errors', 1);
	error_reporting(E_ALL);
	*/
	
	require_once('../includes/configure.php');
	
	// Récupération de l'année de renouvellement dans la base
	$srv = 'ns302986.ovh.net:3306';
	$usr = 'vmagroclient';
	$pwd = 'vmagroclient';
	$db = 'dev_apel_richcore';

	if($_SERVER['HTTP_HOST'] == 'services.apel.fr'){
		$srv = 'ns303892.ovh.net:3306';
		$usr = 'mickael';
		$pwd = 'meuranie';
		$db = 'apel_richcore';
	}
	
	$is_connected = mysql_connect($srv, $usr, $pwd);

	$last_year = date('Y', time()) - 1;
	$sql = 'SELECT 	ur.id_user, u.login
			FROM 	'.$db.'.users_renewall ur,
					'.$db.'.users u
			WHERE	ur.year = "'.$last_year.'"
			AND		ur.id_user = u.id_user';
	$result = mysql_query($sql) or die('get all users > ' . mysql_error() . '<br />' . $sql);

	$users = array();
	while($return = mysql_fetch_assoc($result)) {
		$users[$return['id_user']] = $return['login'];
	}
	
	//echo '<pre>'; var_dump($users); echo '</pre>';exit;
	
	//var_dump($user_renewall_infos['year'] == (date('Y', time()) - 1));
	
	//$user_infos = $SOAP_CLIENT->getUserInformations($user->id);
	foreach($users AS $id_user => $login) {
		// Récupération des groupes de l'utilisateur.
		$sql_groups = '	SELECT 	ug.name
						FROM	'.$db.'.users_to_users_groups utug,
								'.$db.'.users_groups ug
						WHERE	utug.id_user = '.$id_user.'
						AND		utug.id_users_groups = ug.id_users_groups';
		$result_groups = mysql_query($sql_groups) or die('get user\'s groups > ' . mysql_error() . '<br />' . $sql_groups);
		$user_groups = array();
		while($return_groups = mysql_fetch_assoc($result_groups)) {
			$user_groups[] = $return_groups['name'];
		}
	
		$to_check = true;
		$waiting_renew = false;
		$last_year = date('Y', time()) - 1;

		foreach($user_groups AS $group) {
			$temp = explode('_', $group);
			if(isset($temp[0]) and ($temp[0] == 'VALIDATOR' OR $temp[0] == 'administrateur') and $group['group_id'] == '4') {
				$to_check = false;
			}
			// Si en attente de renouvellement
			if(strstr($group, '_waiting_renewall_')) {
				$to_check = false;
				$waiting_renew = true;
			}
		}

		if(!$waiting_renew AND $to_check) {
			// envoie du mail de renouvellement
			echo 'envoie du mail de renouvellemen a '.$login.'<br />';
			sendMail($login, 'renouvellement');
		}
	}
?>
